package com.luoboduner.moo.info.ui.frame;

import com.formdev.flatlaf.extras.FlatSVGUtils;
import com.formdev.flatlaf.util.SystemInfo;
import com.luoboduner.moo.info.App;
import com.luoboduner.moo.info.ui.Init;
import com.luoboduner.moo.info.ui.UiConsts;
import com.luoboduner.moo.info.ui.component.TopMenuBar;
import com.luoboduner.moo.info.ui.listener.FrameListener;
import com.luoboduner.moo.info.util.ComponentUtil;
import com.luoboduner.moo.info.util.IconUtil;
import com.luoboduner.moo.info.util.SystemUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Main Frame
 *
 * @author <a href="https://github.com/rememberber">RememBerBer</a>
 * @since 2021/11/08.
 */
public class MainFrame extends JFrame {
  private static MainFrame context;//单例模式
  private Image normalTrayIcon; // 正常时的任务栏图标
  private Image emptyTrayIcon; // 闪动时的任务栏图标
  private TrayIcon trayIcon;
  private boolean trayFlashing = false;

  public void init() {
    System.setProperty("file.encoding", "UTF-8");
    this.setName(UiConsts.APP_NAME);
    this.setTitle(UiConsts.APP_NAME);
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//        FrameUtil.setFrameIcon(this);
    setIconImages(FlatSVGUtils.createWindowIconImages("/icons/MooInfo.svg"));
    TopMenuBar topMenuBar = TopMenuBar.getInstance();
    topMenuBar.init();
    setJMenuBar(topMenuBar);
    initResource();
    ComponentUtil.setPreferSizeAndLocateToCenter(this, 0.6, 0.8);

    if (SystemUtil.isMacOs() && SystemInfo.isMacFullWindowContentSupported) {
      this.getRootPane().putClientProperty("apple.awt.fullWindowContent", true);
      this.getRootPane().putClientProperty("apple.awt.transparentTitleBar", true);
      this.getRootPane().putClientProperty("apple.awt.fullscreenable", true);
      this.getRootPane().putClientProperty("apple.awt.windowTitleVisible", false);
    }
    FrameListener.addListeners(

    );
  }


  private void initResource() {
    new Thread(new Runnable() {
      @Override
      public void run() {
        initTray();
      }
    }).start();

  }

  /**
   * 初始化系统托盘图标
   */
  private void initTray() {
    SystemTray systemTray = SystemTray.getSystemTray();//获取系统托盘
    try {
      normalTrayIcon = IconUtil.getIcon(this, "/icons/logo-64.png", 20, 20).getImage();

      emptyTrayIcon = IconUtil.getIcon(this, "/icons/ic_launcher_empty.png", 20, 20).getImage();

      trayIcon = new TrayIcon(normalTrayIcon, "daisy监控客户端");
      trayIcon.setImageAutoSize(true);
      trayIcon.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {

          if (Init.configSucceed()) {
            if (e.getButton() == 1) {
              // 显示主窗口
              setVisible(true);
            }

            // 任务栏图标停止闪动
            if (trayFlashing) {
              trayFlashing = false;
              trayIcon.setImage(normalTrayIcon);
            }
          }

          super.mouseClicked(e);
        }
      });

      PopupMenu menu = new PopupMenu();

      //VM Options: -Dfile.encoding=gbk
//      MenuItem exitItem = new MenuItem("退出");
//      exitItem.addActionListener(new ActionListener() {
//        @Override
//        public void actionPerformed(ActionEvent e) {
//          System.exit(1);
//        }
//      });

      MenuItem showItem = new MenuItem("打开监控界面");
      showItem.addActionListener(new ActionListener()
      {
          @Override
          public void actionPerformed(ActionEvent e)
          {
              setVisible(true);
          }
      });
      menu.add(showItem);
//      menu.add(exitItem);

      trayIcon.setPopupMenu(menu);
      systemTray.add(trayIcon);

    }
    catch (AWTException e) {
      e.printStackTrace();
    }
  }

  /**
   * 设置任务栏图标闪动
   */
  public void setTrayFlashing() {
    trayFlashing = true;
    new Thread(new Runnable() {
      @Override
      public void run() {
        while (trayFlashing) {
          try {
            trayIcon.setImage(emptyTrayIcon);
            Thread.sleep(800);

            trayIcon.setImage(normalTrayIcon);
            Thread.sleep(800);

          }
          catch (InterruptedException e) {
            e.printStackTrace();
          }
        }

      }
    }).start();
  }

  public boolean isTrayFlashing() {
    return trayFlashing;
  }

  public static MainFrame getContext() {
    return context;
  }

  @Override
  public void dispose() {
    // 移除托盘图标
    SystemTray.getSystemTray().remove(trayIcon);
    super.dispose();
  }

}
